# -*- coding:utf-8 -*-

from sqlalchemy import Column, String, Integer, VARCHAR
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
try:
    import settings
except:
    from jobs import settings

engine = create_engine(
    'mysql+mysqldb://' + settings.MYSQL_USER + ':' + settings.MYSQL_PASSWD + '@' + settings.MYSQL_HOST + ':' + str(
        settings.MYSQL_PORT) + '/' + settings.MYSQL_DBNAME+"?charset=utf8")
Base = declarative_base()
DBSession = sessionmaker(bind=engine)


class JobModel(Base):
    __tablename__ = 'a51jobs'
    id = Column(Integer, primary_key=True)
    job = Column(VARCHAR(100), nullable=False, index=True)
    job_link = Column(VARCHAR(100), nullable=False)
    company = Column(VARCHAR(100), nullable=False)
    company_link = Column(VARCHAR(100), nullable=False)
    location = Column(VARCHAR(100), nullable=False)
    salary = Column(String(20), nullable=True)
    time = Column(String(20), nullable=True)

    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, self.job)


def create_table():
    Base.metadata.create_all(engine)

def insert():
    model = JobModel(
        job="Android开发工程师",
        job_link="http://www.baidu.com",
        company="百度",
        company_link="http://www.baidu.com",
        location="北京",
        salary="20K",
        time="5-16"
    )
    session = DBSession()
    session.add(model)
    session.commit()
    session.close()

def main():
    create_table()
    insert()

if __name__ == '__main__':
    main()
